<?php
/**
 * 上传文件类型控制 此方法仅限ajax上传使用
 * @param  string   $path    字符串 保存文件路径示例： /Upload/image/
 * @param  string   $format  文件格式限制
 * @param  integer  $maxSize 允许的上传文件最大值 52428800
 * @return booler   返回ajax的json格式数据
 */
function ajax_upload($path='file',$format='empty',$maxSize='52428800'){
    ini_set('max_execution_time', '0');
    // 去除两边的/
    $path=trim($path,'/');
    // 添加Upload根目录
    $path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;
    // 上传文件类型控制
    $ext_arr= array(
        'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
        'photo' => array('jpg', 'jpeg', 'png'),
        'flash' => array('swf', 'flv'),
        'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
        'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')
    );
    if(!empty($_FILES)){
        // 上传文件配置
        $config=array(
            'maxSize'   =>  $maxSize,               // 上传文件最大为50M
            'rootPath'  =>  './',                   // 文件上传保存的根路径
            'savePath'  =>  './'.$path.'/',         // 文件上传的保存路径（相对于根路径）
            'saveName'  =>  array('uniqid',''),     // 上传文件的保存规则，支持数组和字符串方式定义
            'autoSub'   =>  true,                   // 自动使用子目录保存上传文件 默认为true
            'exts'      =>    isset($ext_arr[$format])?$ext_arr[$format]:'',
        );
        // 实例化上传
        $upload=new \Think\Upload($config);
        // 调用上传方法
        $info=$upload->upload();
        $data=array();
        if(!$info){
            // 返回错误信息
            $error=$upload->getError();
            $data['error_info']=$error;
            echo json_encode($data);
        }else{
            // 返回成功信息
            foreach($info as $file){
                $data['name']=trim($file['savepath'].$file['savename'],'.');
                echo json_encode($data);
            }
        }
    }
}

function jiesuan($select,$startdate,$enddate,$type){
    if($type==1){
//        $startdate = date($startdate. ' 12:00:01');
//        $enddate = date( 'Y-m-d 12:00:00',strtotime("$enddate +1 days"));
        $data['bind_did'] = $select;
        $data['time'] = array('between',array($startdate,$enddate));
		$map['createtime'] = array('between',array($startdate,$enddate));
        $list_wages = D("ImsAchievement")->join('right join register on register.bind_id=ims_achievement.jsmc and register.bind_did=ims_achievement.jg_dm')->field('register.id,register.bind_id,register.bind_did,register.bind_name,sum(ims_achievement.tcje) as wages')->where('bind_did='.$select)->where($map)->group('register.id')->select();
        foreach ($list_wages as $key =>$value){
            $list = M('register')->join('join bonus on register.id=bonus.uid')->field('bonus.uid,bind_name,register.tel as tel,count(bonus.uid) as count,sum(bonus.money) as zmoney')->where('uid='.$value['id'])->where($data)->group('register.id')->find();
            $list_wages[$key]['zmoney']=$list['zmoney'];
            $list_wages[$key]['jsmc']=$list['bind_id'];
            $list_wages[$key]['tel']=$list['tel'];
            $list_wages[$key]['count']=$list['count'];
        }
    }else{
//        $startdate = date($startdate. ' 00:00:01');
//        $enddate = date($enddate. ' 23:59:59');
        $data['ctime'] = array('between',array(strtotime($startdate),strtotime($enddate)));
        $list_wages = M('register')->field('id,bind_id,bind_did,bind_name,tel')->where('bind_did='.$select)->group('id')->select();
        //var_dump($list_wages);die;
        foreach ($list_wages as $key => $value){
            $list = M('pbonus')->join('register on pbonus.uid=register.id')->field('register.id,count(uid) as count,sum(moneys) as zmoney')->where('uid='.$value['id'])->where($data)->group('register.id')->select();
            $list_wages[$key]['zmoney']=$list[0]['zmoney'];
            $list_wages[$key]['count']=$list[0]['count'];
        }
    }

    return $list_wages;

}